<?php

//活动报名
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ApplyController extends Controller{

    //获取点击报名
    public function applylike($release_id){
        $data = DB::table('apply')
            ->leftjoin('user','apply.apply_user','=','user.user_id')
            ->where('apply_activity',$release_id)
            ->get();
        if($data == true){
            foreach ($data as $value){
                $heads = $value->head;
                $user_id = $value->nickname;
                $time    = $value ->apply_time;
                $lk[] = array('head' => $heads, 'nickname' => $user_id,'time'=>$time);
            }
            return $lk;
        }else{
            return [];
        }
    }

    public function applys(Request $request){
        if($request ->isMethod('post')){}
    }

    //报名与取消报名
    public function apply(Request $request){
        if($request ->isMethod('post')){
            $activity_id          = $request ->input('activity_id');
            $user_id              = $request ->input('user_id');
            $select        = DB::table('apply')
                ->where('apply_user','=',$user_id)
                ->where('apply_activity','=',$activity_id)
                ->first();
            if($select  == true){
             $delete  =   DB::table('apply') ->where('apply_user','=',$user_id)
                    ->where('apply_activity','=',$activity_id) ->delete();
             if($delete == true){
                 return json::json('success','取消报名',json::value(1));
             }else{
                 return json::json('failure','由于网络或者不正当原因取消报名失败',json::value(1));
             }
            }else{
                $insert    = DB::table('apply')->insert([
                             'apply_user'           => $user_id,
                             'apply_activity'        => $activity_id,
                             'apply_time'             => date('Y-m-d H:i:s')
                ]);
                if($insert  == true){
                    return json::json('success','报名成功',json::value(1));
                }else{
                    return json::json('failure','由于网络或者不正当原因报名失败',json::value(1));
                }
            }

        }
    }

    public function AllActivity(Request $request,$type){
        if($request ->isMethod('post')){
            $village_id       = $request ->input('village_id');
            $time= date('Y-m-d H:i:s',time());
            if($type ==1){
                $data   = DB::table('user')
                    ->select('activity_id','head','nickname','activity_create_time','headline','site','start_time','end_time','attend')
                    ->join('activity','user.user_id','=','activity.user_activity')
                    ->where('end_time','<',$time)
                    ->where('user_village',$village_id)
                    ->get();

                if ($data == true) {
                    return json::json('success', '获取正在进行中活动成功', $data);
                } else {
                    return json::json('failure', '获取正在进行中失败', json::value(2));
                }
                return json::json('failure', '获取失败', json::value(2));
            }else{
                $data   = DB::table('user')
                    ->select('activity_id','head','nickname','activity_create_time','headline','site','start_time','end_time','attend')
                    ->join('activity','user.user_id','=','activity.user_activity')
                    ->where('end_time','>',$time)
                    ->where('user_village',$village_id)
                    ->get();
                if ($data == true) {
                    return json::json('success', '获取结束的活动成功', $data);
                } else {
                    return json::json('failure', '获取结束的活动失败', json::value(2));
                }
                return json::json('failure', '获取失败', json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    public function applysum($activity_id){
            $select        = DB::table('apply')
                           ->where('apply_activity',$activity_id)
                           ->count();
            return $select;
    }

}
